<template>
  <u-popup :show="show" round="10px" @close="cancel" :closeOnClickOverlay="true" :closeable="true" mode="bottom">
    <div class="post_demand_popup">
      <div class="title">请选择您的需求</div>
      <div class="content">
        <div class="wrap wrap1">
          <button class="item_btn" v-if="(!isLogin && userPubStatus && !userPubStatus.job_status) || !userPubStatus" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber1">
            <image src="@/static/images/common/fbzp.png" mode="scaleToFill" />
            <p>发布招聘</p>
          </button>
          <div class="item" v-else-if="isLogin && userPubStatus && !userPubStatus.job_status" @click="clickItem(1, 'fb')">
            <image src="@/static/images/common/fbzp.png" mode="scaleToFill" />
            <p>发布招聘</p>
          </div>
          <div class="item" v-else-if="isLogin && userPubStatus && userPubStatus.job_status" @click="clickItem(1, 'gl')">
            <image src="@/static/images/common/fbzp.png" mode="scaleToFill" />
            <p>管理招聘</p>
          </div>
          <button
            class="item_btn"
            v-if="(!isLogin && userPubStatus && !userPubStatus.resume_status) || !userPubStatus"
            open-type="getPhoneNumber"
            @getphonenumber="getPhoneNumber2"
          >
            <image src="@/static/images/common/fbqz.png" mode="scaleToFill" />
            <p>发布求职</p>
          </button>
          <div class="item" v-else-if="isLogin && userPubStatus && !userPubStatus.resume_status" @click="clickItem(2, 'fb')">
            <image src="@/static/images/common/fbqz.png" mode="scaleToFill" />
            <p>发布求职</p>
          </div>
          <div class="item" v-else-if="isLogin && userPubStatus && userPubStatus.resume_status" @click="clickItem(2, 'gl')">
            <image src="@/static/images/common/fbqz.png" mode="scaleToFill" />
            <p>管理求职</p>
          </div>
        </div>
        <div class="wrap">
          <div class="item" @click="clickItem(3)">
            <image src="@/static/images/common/zzzd2.png" mode="scaleToFill" />
            <p>招租转店</p>
          </div>
          <div class="item" @click="clickItem(4)">
            <image src="@/static/images/common/wxzd.png" mode="scaleToFill" />
            <p>我想找店</p>
          </div>
          <div class="item" @click="clickItem(5)">
            <image src="@/static/images/common/cyfw.png" mode="scaleToFill" />
            <p>餐饮服务</p>
          </div>
        </div>
      </div>
    </div>
  </u-popup>
</template>

<script>
export default {
  name: "postDemand",
  props: {
    show: {
      type: Boolean,
      default: true,
    },
    isrefresh: {
      type: Object,
    },
  },
  data() {
    return {
      isLogin: false,
      phone: "",
      userStatus: null, //resume_status,job_status
    };
  },
  computed: {
    userPubStatus() {
      if (this.isrefresh) {
        this.getUserStatus();
      }
      return this.userStatus;
    },
  },
  methods: {
    async submit() {
      this.$emit("submit");
    },
    cancel() {
      this.$emit("cancel");
    },
    clickItem(type, type2) {
      switch (type) {
        case 1:
          if (type2 == "gl") {
            this.$togo("/pages2/regulateJobs");
          } else {
            this.$togo("/pages2/postOffer", { phone: this.phone });
          }
          this.$emit("cancel");
          break;
        case 2:
          if (type2 == "gl") {
            this.$togo("/pages2/regulateResume");
          } else {
            this.$togo("/pages2/postRecruitmentInformation", { phone: this.phone });
          }
          this.$emit("cancel");
          break;
        case 3:
          this.$layer.message("功能正在开发中,敬请期待...");
          break;
        case 4:
          this.$layer.message("功能正在开发中,敬请期待...");
          break;
        case 5:
          this.$layer.message("功能正在开发中,敬请期待...");
          break;
      }
    },
    async getPhoneNumber1(e) {
      let _this = this;
      let deviceModel = await this.getSysteminfo();

      if (e.detail.errMsg == "getPhoneNumber:ok") {
        console.log(e.detail.code);
        uni.login({
          provider: "weixin",
          success: async res => {
            console.log(res.code);
            this.code = res.code;
            let params = {
              code: e.detail.code,
              loginCode: res.code,
              device_model: deviceModel,
            };
            if (_this.$getStoraged("shareUserId").length) {
              params.invite_id = JSON.parse(_this.$getStoraged("shareUserId"));
            }
            let res2 = await this.$http.requestSync("/wechat/login", params);
            console.log(res2.header);
            this.$layer.message(res2.data.user_info.phone);
            this.$setStoraged("phone", res2.data.user_info.phone);
            this.$setStoraged("user_id", res2.data.user_info.id);
            this.$setStoraged("authration_token", res2.data.token);
            this.$setStoraged("open_id", res2.data.user_info.wechart_open_id);
            this.getUserStatus();
            setTimeout(() => {
              if (this.$getStoraged("phone").length > 0) {
                this.isLogin = true;
                this.phone = JSON.parse(this.$getStoraged("phone"));
              } else {
                this.isLogin = false;
              }
              this.$emit("reload");
              this.$togo("/pages2/postOffer", { phone: res2.data.user_info.phone });
            }, 0);
          },
          fail: e => {
            console.log(1, e);
          },
        });
      } else {
        this.$togo("/pages2/postOffer");
      }
    },
    async getPhoneNumber2(e) {
      let _this = this;
      let deviceModel = await this.getSysteminfo();
      if (e.detail.errMsg === "getPhoneNumber:ok") {
        uni.login({
          provider: "weixin",
          success: async res => {
            this.code = res.code;
            // console.log(res);
            let params = {
              code: e.detail.code,
              loginCode: res.code,
              device_model: deviceModel,
            };
            if (_this.$getStoraged("shareUserId")) {
              params.invite_id = JSON.parse(_this.$getStoraged("shareUserId"));
            }
            let res2 = await this.$http.requestSync("/wechat/login", params);
            this.$layer.message(res2.data.user_info.phone);
            this.$setStoraged("phone", res2.data.user_info.phone);
            this.$setStoraged("user_id", res2.data.user_info.id);
            this.$setStoraged("authration_token", res2.data.token);
            this.$setStoraged("open_id", res2.data.user_info.wechart_open_id);
            this.getUserStatus();
            setTimeout(() => {
              if (this.$getStoraged("phone").length > 0) {
                this.isLogin = true;
                this.phone = JSON.parse(this.$getStoraged("phone"));
              } else {
                this.isLogin = false;
              }
              this.$emit("reload");
              this.$togo("/pages2/postRecruitmentInformation", { phone: res2.data.user_info.phone });
            }, 0);
          },
        });
      } else {
        this.$togo("/pages2/postRecruitmentInformation");
      }
    },
    // 查询用户是否发布过求职和招聘
    async getUserStatus() {
      let res = await this.$http.requestSync("/user/status");
      if (res.code == 200) {
        this.userStatus = res.data;
      }
      console.log("getUserStatusgetUserStatusgetUserStatus", res);
    },
    getSysteminfo() {
      return new Promise(resolve => {
        uni.getSystemInfo({
          success: res => {
            this.device_model = res.deviceModel;
            resolve(res.deviceModel);
          },
        });
      });
    },
  },
  watch: {},
  created() {
    if (this.$getStoraged("phone").length > 0) {
      this.isLogin = true;
      this.phone = JSON.parse(this.$getStoraged("phone"));
    } else {
      this.isLogin = false;
    }
    this.getUserStatus();
  },
  // 组件周期函数--监听组件挂载完毕
  mounted() {},
  // 组件周期函数--监听组件数据更新之前
  beforeUpdate() {},
  // 组件周期函数--监听组件数据更新之后
  updated() {},
  // 组件周期函数--监听组件激活(显示)
  activated() {},
  // 组件周期函数--监听组件停用(隐藏)
  deactivated() {},
  // 组件周期函数--监听组件销毁之前
  beforeDestroy() {},
};
</script>

<style scoped lang="scss">
.post_demand_popup {
  height: 324px;
  box-sizing: border-box;
  padding: 15px 21px;
  text-align: center;
  .title {
    font-size: 16px;
    font-family: PingFang SC-Semibold, PingFang SC;
    font-weight: 600;
    color: #333333;
  }
  .content {
    margin: 24px auto 0;
    .wrap {
      display: flex;
      justify-content: space-around;
      margin-bottom: 30px;
      .item {
        image {
          width: 71px;
          height: 71px;
        }
        ._p {
          font-size: 14px;
          font-family: PingFang SC-Regular, PingFang SC;
          font-weight: 400;
          color: #333333;
        }
      }
      .item_btn {
        width: 71px;
        height: 95px;
        border: none;
        background: transparent;
        padding: 0;
        image {
          width: 71px;
          height: 71px;
        }
        ._p {
          font-size: 14px;
          font-family: PingFang SC-Regular, PingFang SC;
          font-weight: 400;
          color: #333333;
          margin-top: -17px;
        }
        &::after {
          content: "";
          border: none;
          box-shadow: none;
        }
      }
    }
    .wrap1 {
      width: 70%;
      margin: 0 auto;
      margin-bottom: 30px;
    }
  }
}
</style>
